package com.yyw.gulimall.order.controller;

import com.yyw.gulimall.order.entity.OrderEntity;
import com.yyw.gulimall.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * @Classname TestController
 * @Description RabbitMQ 消息发送接收测试
 * @Date 2020/12/23 16:46
 * @Created yyw
 */
@RestController("/test")
public class TestController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送消息
     *
     * @param num
     * @return
     */
    @GetMapping("/accept/{num}")
    public String accecp(@PathVariable("num") Integer num) {
        for (Integer i = 0; i < num; i++) {
            OrderReturnReasonEntity reasonEntity = OrderReturnReasonEntity.builder()
                    .createTime(new Date())
                    .name("测试AA" + i)
                    .sort(8)
                    .status(1)
                    .build();
            String uuid = UUID.randomUUID().toString();
            if (i%2 == 0) {
                rabbitTemplate.convertAndSend("java.exchange", "java.queue", reasonEntity, new CorrelationData(uuid));
            }else {
                // 我们故意写错Queue的名字，模拟Exchange 投递Queue 失败场景
                rabbitTemplate.convertAndSend("java.exchange", "java.queue", reasonEntity, new CorrelationData(uuid));
            }
        }
        return "ok";
    }

    /**
     * 发送消息
     *
     * @param num
     * @return
     */
    @GetMapping("/accept/many/{num}")
    public String accecpMany(@PathVariable("num") Integer num) {
        for (Integer i = 0; i < num; i++) {
            if (i % 2 == 0) {
                OrderReturnReasonEntity reasonEntity = OrderReturnReasonEntity.builder()
                        .createTime(new Date())
                        .name("测试AA" + i)
                        .sort(8)
                        .status(1)
                        .build();
                rabbitTemplate.convertAndSend("java.exchange", "java.queue", reasonEntity);
            } else {
                OrderEntity entity = OrderEntity.builder()
                        .autoConfirmDay(1)
                        .billContent("AA")
                        .billHeader("AA")
                        .billReceiverEmail("DD")
                        .billReceiverPhone("测试")
                        .commentTime(new Date())
                        .deliveryCompany("测试的海口市但是肯定就是看")
                        .build();
                rabbitTemplate.convertAndSend("java.exchange", "java.queue", entity);
            }
        }
        return "ok";
    }
}
